package com.example.gametestproject;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Point;
import android.os.PowerManager;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.gametestproject.GameView;

import java.util.Vector;

public class MainActivity extends Activity
{
	public GameView mpView;
	FrameLayout fLayout;
	View mainView;
    PowerManager.WakeLock wakeLock;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//get screen width and screen height
		this.getSystemScreenSize();
		fLayout = new FrameLayout(this);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        PowerManager pManager = (PowerManager)this.getSystemService(POWER_SERVICE);
        wakeLock = pManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "mainActivity");
        new LoadingResourceTask().execute();

		Log.i("MainActivity", "onCreate");
	}
	Button.OnClickListener clickListener = new Button.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			mpView = new GameView(MainActivity.this);
			fLayout.removeView(mainView);
			mainView = null;
			fLayout.addView(mpView,ResourceManager.getInstance().getScreenWidth(), ResourceManager.getInstance().getScreenHeight());
		}
	};
    //load resources task
    private class LoadingResourceTask extends AsyncTask<Void, Integer, Void>
    {
    	private View loadScreenView;
        private TextView progressView;
        private ProgressBar progressBar;
        @Override
        protected void onPreExecute() {
        	LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(MainActivity.this.LAYOUT_INFLATER_SERVICE);
        	loadScreenView = inflater.inflate(R.layout.loadingscreen, fLayout, false);
        	fLayout.addView(loadScreenView);
            MainActivity.this.setContentView(fLayout);
            progressView = (TextView)findViewById(R.id.tv_loadingtext);
            progressBar = (ProgressBar)findViewById(R.id.pb_progressbar);
            progressBar.setMax(100);
        }
        @Override
        protected Void doInBackground(Void... objects) {
            try
            {
                //Get the current thread's token
                synchronized (this)
                {
                    ResourceManager manager =  ResourceManager.getInstance();
                    manager.setContext(MainActivity.this);
                    int count = 0;
                    while(count < manager.mSpriteResId.size())
                    {
                        if(manager.mSpriteBitmaps == null)
                        {
                            manager.mSpriteBitmaps = new Vector<Bitmap>();
                        }
                        Bitmap bitmap = manager.getBitmap(manager.mSpriteResId.get(count), manager.mScaleWidth, manager.mScaleHeight);
                        manager.mSpriteBitmaps.addElement(bitmap);
                        count++;
                        publishProgress((int)((count*1.0 / manager.mSpriteResId.size())*100));
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
           // super.onProgressUpdate(values);
        	 progressView.setText("Progress: " + Integer.toString(values[0]) + "%");  
             progressBar.setProgress(values[0]);
        }

        @Override
        protected void onPostExecute(Void o) {
            //super.onPostExecute(o);
            LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(MainActivity.this.LAYOUT_INFLATER_SERVICE);
            mainView = inflater.inflate(R.layout.mainscreenview, fLayout, false);
            fLayout.removeView(loadScreenView);
            fLayout.addView(mainView);
            Button button = (Button)MainActivity.this.findViewById(R.id.button);
            button.setOnClickListener(clickListener);
        }
    }

	@SuppressLint("NewApi")
	private void getSystemScreenSize()
	{
		Display display = this.getWindowManager().getDefaultDisplay();
		if(android.os.Build.VERSION.SDK_INT > 13)
		{
			Point size = new Point();
			display.getSize(size);
			ResourceManager.getInstance().setScreenSize(size.x, size.y);
		}
		else
		{
			ResourceManager.getInstance().setScreenSize(display.getWidth(), display.getHeight());
		}
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

    @Override
    protected void onPause()
    {
        super.onPause();
        if (this.mpView != null)
            this.mpView.pause();
        wakeLock.release();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (this.mpView != null)
            this.mpView.resume();
        wakeLock.acquire();
    }
}
